package com.yyj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyj.dao.EmployeeDao;
import com.yyj.entity.Admin;
import com.yyj.entity.Employee;
import com.yyj.entity.Params;
import com.yyj.exception.CustomException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    final
    EmployeeDao employeeDao;

    public EmployeeService(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }


    public List<Employee> getEmployee() {
        return employeeDao.getEmployee();
    }

    public PageInfo<Employee> getBySearch(Params params) {
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //裸数据 不包括数据总数,当前页和页面大小
        List<Employee> list = employeeDao.getBySearch(params);
        //封装在一起的数据
        return PageInfo.of(list);
    }

    public void insertEmployee(Employee employee) {

        employeeDao.insertEmployee(employee);
    }

    public PageInfo<Employee> getEmployeesWithPostIdOne(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //裸数据 不包括数据总数,当前页和页面大小
        List<Employee> list= employeeDao.getEmployeesByPostIdOne(params);
        //封装在一起的数据
        return PageInfo.of(list);
    }

    public void updateEmployee(Employee employee) {
        employeeDao.updateEmployee(employee);
    }


    public void delete(Integer id) {
        employeeDao.deleteByPrimaryKey(id);
    }

    public Employee login(Employee employee) {
        //非空判断
        if (employee.getName() == null || employee.getName().isEmpty()) {
            throw new CustomException("用户名不能为空");
        }
        if (employee.getPassword() == null || employee.getPassword().isEmpty()) {
            throw new CustomException("密码不能为空");
        }
        Employee loginUser = employeeDao.login(employee);
        if (loginUser==null) {
            throw new CustomException("用户名或密码错误");
        }
        return loginUser;
    }
    public void add(Employee employee) {
        if (employee.getName() == null ||"" .equals(employee.getName())) {
            throw new CustomException("用户名不能为空");
        }
        //添加管理员的重复性判断
        Employee user = employeeDao.findByName(employee.getName());
        if (user != null) {
            throw new CustomException("该用户名已经存在，请更换");
        }
        if(employee.getPassword()== null){
            employee.setPassword("123456");
            //已经有了，提示前台

        }
        employeeDao.insert(employee);

    }

    public Employee getInfo(Integer id) {
        Employee employee = employeeDao.getInfo(id);
        return employee;
    }
}
